﻿// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	: FI.Net Application Framework
// COMPONENT ID	: FI.Common
// COMPONENT DESC:		
//
// CREATED DATE/BY:		18 Aug 2018 / Tian Mi
//
// REVISION HISTORY:	DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using FI.Core;

namespace FI.Core
{
    /// <summary>
    /// 
    /// </summary>
    public static class DateTimeModeHelper
    {
        /// <summary>
        /// Sets the UTC.
        /// </summary>
        /// <param name="table">The table.</param>
        public static void SetUtc(DataTable table)
        {
            Set(table, DataSetDateTime.Utc);
        }

        /// <summary>
        /// Sets the local.
        /// </summary>
        /// <param name="table">The table.</param>
        public static void SetLocal(DataTable table)
        {
            Set(table, DataSetDateTime.Local);
        }

        /// <summary>
        /// Sets the unspecified local.
        /// </summary>
        /// <param name="table">The table.</param>
        public static void SetUnspecifiedLocal(DataTable table)
        {
            Set(table, DataSetDateTime.UnspecifiedLocal);
        }

        /// <summary>
        /// Sets the unspecified.
        /// </summary>
        /// <param name="table">The table.</param>
        public static void SetUnspecified(DataTable table)
        {
            Set(table, DataSetDateTime.Unspecified);
        }

        /// <summary>
        /// Sets the specified table.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <param name="mode">The mode.</param>
        public static void Set(DataTable table, DataSetDateTime mode)
        {
            if (null == table)
            {
                return;
            }
            foreach (DataColumn column in table.Columns)
            {
                if (column.DataType == typeof(DateTime))
                {
                    column.DateTimeMode = mode;
                }
            }
        }


        /// <summary>
        /// Converts the specified table.
        /// </summary>
        /// <param name="table">The table.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static void Convert(DataTable table)
        {
            try
            {
                if (TimeConverter.NeedConvertUtc)
                {
                    SetUtc(table);
                }
            }
            catch
            {
                // ignore cofiguration cannot found exceptin when design-time
            }
        }
    }
}
